package com.wonderland.webtest.datasource;

import com.wonderland.webtest.common.SpringContextHolder;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gaoxy on 2015-09-14 0014.
 */
public class DynamicDataSource extends AbstractRoutingDataSource {


    @PostConstruct
    private void initDataSource() {
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("lamb", SpringContextHolder.getBean("lamb"));
        dataSourceMap.put("smart", SpringContextHolder.getBean("smart"));
        setTargetDataSources(dataSourceMap);
    }


    protected Object determineCurrentLookupKey() {
        // 在进行DAO操作前，通过上下文环境变量，获得数据源的类型
        return DataSourceContextHolder.getDataSourceType();
    }
}
